package com.itheima.service.impl;


import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingServie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class OrderSettingServieImpl implements OrderSettingServie {
    @Autowired
    private OrderSettingServie orderSettingServie;
    @Autowired
    private OrderSettingDao orderSettingDao;


    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String dateBegin=date+"-1";
        String dateEnd=date+"-31";
        Map map=new HashMap();
        map.put("begin",dateBegin);
        map.put("end",dateEnd);
        List<OrderSetting> list=orderSettingDao.getOrderSettingByMonth(map);
         List<Map> data = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Map<String,Object> m=new HashMap<>();
            m.put("date",orderSetting.getOrderDate().getDate());
            m.put("number",orderSetting.getNumber());
            m.put("reservations",orderSetting.getReservations());
            data.add(m);
        }





        return data;
    }
/*
*
* 批量导入
* */
    @Override
    public void add(List<OrderSetting> lsit) {
        if (lsit!=null && lsit.size()>0) {
            for (OrderSetting orderSetting : lsit) {
                long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if(count>0){
                    orderSettingDao.editNumber(orderSetting);

                }else{
                    //不存在执行,添加
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        Date orderDate = orderSetting.getOrderDate();
        long count = orderSettingDao.findCountByOrderDate(orderDate);
        if (count>0){
            orderSettingDao.editNumber(orderSetting);
        }else {
            orderSettingDao.add(orderSetting);
        }
    }
}
